var fs = require('fs')
var student = require('./students')

var express = require('express')
//创建路由容器
var router = express.Router()
//把路由都挂载到router路由器中

router.get('/', function (req, res) {
    student.find(function (err, students) {
        if (err) {
            return res.end.status(500).send('Server error.')
        }
        res.render('index.html', {
            students: students
        })
    })
})

router.get('/add', function (req, res) {
    res.render('./add.html')
})

router.post('/add', function (req, res) {
    student.add(req.body, function (err) {
        if (err) {
            return res.end.status(500).send('Server error.')
        }
        res.redirect('/')
    })
})

router.get('/updata', function (req, res) {
    student.findById(parseInt(req.query.id), function (err, student) {
        if (err) {
            return res.end.status(500).send('Server error.')
        }
        res.render('./updata.html', {
            student: student
        })
    })
})

router.post('/updata', function (req, res) {
    student.updataById(req.body, function (err) {
        if (err) {
            return res.end.status(500).send('Server error.')
        }
        res.redirect('/')
    })
})
router.get('/delete', function (req, res) {
    student.deleteById(parseInt(req.query.id), function (err) {
        if (err) {
            return res.end.status(500).send('Server error.')
        }
        res.redirect('/')
    })
})

module.exports = router

